#include "name.hpp"

std::map<int, std::string> name_map;
int name_index = 0;

std::map<int, std::string> get_name_map() {
    return name_map;
}

static void getAllFiles(std::vector<std::string>& files)
{
    files = std::vector<std::string>{
                                    "Arabic.txt"
                                    ,"Chinese.txt"
                                    ,"Czech.txt"
                                    ,"Dutch.txt"
                                    ,"English.txt"
                                    ,"French.txt"
                                    ,"German.txt"
                                    ,"Greek.txt"
                                    ,"Irish.txt"
                                    ,"Italian.txt"
                                    ,"Japanese.txt"
                                    ,"Korean.txt"
                                    ,"Polish.txt"
                                    ,"Portuguese.txt"
                                    ,"Russian.txt"
                                    ,"Scottish.txt"
                                    ,"Spanish.txt"
                                    ,"Vietnamese.txt"
                                    };
    for (int i=0; i < files.size(); i++) {
        name_map.insert(std::make_pair(i, files[i]));
    }
}

static void getNames(const std::string& file_path, std::vector<std::string>& names)
{
    FILE *f = fopen(file_path.c_str(), "rt");
    
    for (;;)
    {
        char buf[1024];
        if (!fgets(buf, sizeof(buf) - 3, f))
            break;
        size_t len = strlen(buf);
        buf[len - 1] = 0;
        names.push_back(std::string(buf));
    }
    fclose(f);
}

std::vector<std::pair<int, std::string>> getAllName(const std::string& dir_path) {
    std::vector<std::string> files;
    std::vector<std::string> names;
    std::vector<std::pair<int, std::string>> result;
    getAllFiles(files);
    for (int i=0;i<files.size();i++) {
        std::string file_path = dir_path+files[i];
        getNames(file_path, names);
        for(int j=0;j<names.size();j++){
            result.push_back(make_pair(i, names[j]));
        }
        names.clear();
    } 
    return result;
}